<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading"
              style="width: 100%;">
      <el-table-column prop="gmtCreate" header-align="center" align="center" label="创建时间">
      </el-table-column>
      <el-table-column prop="materialCode" header-align="center" align="center" label="素材Code">
      </el-table-column>
      <el-table-column prop="materialName" header-align="center" align="center" label="素材名">
      </el-table-column>
      <el-table-column prop="materialFormat" header-align="center" align="center" label="格式">
      </el-table-column>
      <el-table-column prop="materialWidth" header-align="center" align="center" label="宽(px)">
      </el-table-column>
      <el-table-column prop="materialHeight" header-align="center" align="center" label="高(px)">
      </el-table-column>
      <el-table-column prop="materialUseRate" header-align="center" align="center" label="使用率">
      </el-table-column>
      <el-table-column prop="materialLabel" header-align="center" align="center" label="标签">
      </el-table-column>
      <el-table-column prop="materialImageUrl" header-align="center" align="center" label="预览图">
        <template slot-scope="scope">
          <div class="image-container">
            <img :src="scope.row.materialImageUrl" class="preview-image" @click="imagePreviewHandle(scope.row.materialImageUrl)"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="materialCategoryCode" header-align="center" align="center" label="分类code">
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
                   :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
                   layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 查看大图 -->
    <ImagePreview v-if="imagePreviewVisible" ref="imagePreview"></ImagePreview>
  </div>
</template>

<script>
import ImagePreview from '@/views/common/image-preview'

export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      imagePreviewVisible: false
    }
  },
  components: {
    ImagePreview
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/material/material/queryWormholeGalaxyMaterialsByEs'),
        method: 'post',
        data: this.$http.adornData({
          isPage: true,
          pageNumb: this.pageIndex,
          pageSize: this.pageSize,
          keyword: this.dataForm.key
        })
      }).then(({ data }) => {
        if (data && data.success) {
          this.dataList = data.data
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 图片预览
    imagePreviewHandle(imageUrl) {
      this.imagePreviewVisible = true
      this.$nextTick(() => {
        this.$refs.imagePreview.init(imageUrl)
      })
    }
  }
}
</script>

<style scoped>
.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.preview-image {
  max-width: 80px;
  max-height: 80px;
  cursor: pointer;
  border-radius: 4px;
}

.preview-image:hover {
  opacity: 0.8;
}
</style>
